
package address;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
 * 定义联系人的类，包含联系人的各种信息
 * @author Mavis
 */
public class People implements Serializable{
    String name=null;     //姓名
    String month=null;  //生日
    String year=null;
    String day=null;
    ArrayList phone=new ArrayList();   //手机号码
    ArrayList email=new ArrayList();   //电子邮箱
    ArrayList im=new ArrayList();     //即时通讯工具
    ArrayList belongs=new ArrayList();    //所属组
    String photo=null;    //头像
    String home;     //家庭地址
    String workplace;     //工作地址
    String homepage;    //个人主页
    String remark;    //备注
    String postcode;     //邮编
    
    
    public People(String name){   //创建新联系人
      this.name=name; 
      
    }
    
    public People(){
        
    }
    String getName(){    // 返回联系人姓名
        return name;
    }
    void setName(String name){    //设置联系人的名字
        this.name=name;
    }
    
    
    void setHome(String home){   //设置家庭地址
       this.home=home;
    }
    String getHome(){     //返回家庭地址
        return home;
    }
   
    void setWorkplace(String workplace){    //设置家庭地址
        this.workplace=workplace;
    }
     String getWorkplace(){     //返回家庭地址
         return workplace; 
     }
     
     
     
     void creatEmail(String e){   //创建新的电子邮箱
         email.add(e);
     }
     boolean deletEmail(String e){   //删除某个电子邮箱
         return email.remove(e);
     }
     
     void setPhoto(String photo){    //设置联系人头像
         this.photo=photo;
     }
     String getPhoto(){     //返回联系人头像
         return photo;
     }
     
     void setHomepage(String homepage){    //设置个人主页
         this.homepage=homepage;
     }
     String getHomepage(){    //返回个人主页
         return homepage;
     }
     
     void setRemark(String remark){    //设置备注
         this.remark=remark;
     }
     String getRemark(){    //返回备注
         return remark;
     }
     
     void setPostcode(String postcode){    //设置邮编
         this.postcode=postcode;
     }
     String getPostcode(){    //返回邮编
         return postcode;
     }
     
     void creatIm(String style,String number){    //创建新的即时通讯工具
         IM Im=new IM(style,number);
         im.add(Im);
     }
     void deletIm(String style,String number){    //删除某个即时通讯工具
         IM Im;
         int i;
         i=im.size();
         for(int j=0;j<i;j++){
             Im=(IM) im.get(j);
             if(Im.style.equals(style)&&Im.number.equals(number)){
                 im.remove(j);
             }
         }
     }
     
     void creatBelongs(String Belongs){    //创建新的所属组
         belongs.add(Belongs);
     }
     void deletBelongs(String Belongs){    //删除某个所属组
         belongs.remove(Belongs);
     }
     
     
     /**
     * 字符串集合转换字符串(逗号分隔)
     */
    String makeStringByStringSet(Set<String> stringSet) {
        StringBuilder str = new StringBuilder();
        int i = 0;
        for (String s : stringSet) {
            if (i == stringSet.size() - 1) {
                str.append(s);
            } else {
                str.append(s + ",");
            }
            i++;
        }
        return str.toString().toLowerCase();
    }

    /**
     * 获取拼音集合
     * @return Set<String>
     */
    public static Set<String> getPinyin(String src) {
        if (src != null && !src.trim().equalsIgnoreCase("")) {
            char[] srcChar;
            srcChar = src.toCharArray();
            //汉语拼音格式输出类  
            HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();

//输出设置，大小写，音标方式等  
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

            String[][] temp = new String[src.length()][];
            for (int i = 0; i < srcChar.length; i++) {
                char c = srcChar[i];
                //是中文或者a-z或者A-Z转换拼音
                if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {
                    try {
                        temp[i] = PinyinHelper.toHanyuPinyinStringArray(srcChar[i], hanYuPinOutputFormat);
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                    }
                } else if (((int) c >= 65 && (int) c <= 90) || ((int) c >= 97 && (int) c <= 122)) {
                    temp[i] = new String[]{String.valueOf(srcChar[i])};
                } else {
                    temp[i] = new String[]{""};
                }
            }
            String[] pingyinArray = Exchange(temp);
            Set<String> pinyinSet = new HashSet<String>();
            for (int i = 0; i < pingyinArray.length; i++) {
                pinyinSet.add(pingyinArray[i]);
            }
            return pinyinSet;
        }
        return null;
    }

    /**
     * 递归
     *
     * @author wyh
     */
    public static String[] Exchange(String[][] strJaggedArray) {
        String[][] temp = DoExchange(strJaggedArray);
        return temp[0];
    }

    /**
     * 递归
     * @return
     */
    private static String[][] DoExchange(String[][] strJaggedArray) {
        int len = strJaggedArray.length;
        if (len >= 2) {
            int len1 = strJaggedArray[0].length;
            int len2 = strJaggedArray[1].length;
            int newlen = len1 * len2;
            String[] temp = new String[newlen];
            int Index = 0;
            for (int i = 0; i < len1; i++) {
                for (int j = 0; j < len2; j++) {
                    temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j];
                    Index++;
                }
            }
            String[][] newArray = new String[len - 1][];
            for (int i = 2; i < len; i++) {
                newArray[i - 1] = strJaggedArray[i];
            }
            newArray[0] = temp;
            return DoExchange(newArray);
        } else {
            return strJaggedArray;
        }
    }

}
